package com.szt.sy.controller.system.area;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.plugins.Page;
import com.szt.sy.anontation.Authority;
import com.szt.sy.anontation.AuthorityConfig;
import com.szt.sy.anontation.SysLog;
import com.szt.sy.common.AuthorityCommon;
import com.szt.sy.controller.base.AdminBaseController;
import com.szt.sy.entity.system.Area;
import com.szt.sy.service.system.area.AreaService;
import com.szt.sy.util.id.generator.IDUtils;
import com.szt.sy.vo.rep.base.RepBaseVO;

/***
 * 地区-控制器
 * 
 * @author wwy
 *
 */
@Controller
@AuthorityConfig(prefix = "/admin/area/list")
@RequestMapping(value = "admin/area")
public class AreaController extends AdminBaseController {
	
	@Autowired
	private AreaService areaService;
	
	/**
	 * 去列表页面
	 *
	 * @param
	 * @throws Exception
	 */
	@Authority(name = AuthorityCommon.LIST)
	@RequestMapping(value = "/goPage")
	public ModelAndView goPage() throws Exception {
		ModelAndView mv = this.getModelAndView();
		Integer totleCount = areaService.findCount();
		mv.addObject("totleCount", totleCount);
		mv.setViewName("system/area/area_list");
		
		return mv;
	}
	
	/**
	 * 保存
	 *
	 * @param
	 * @throws Exception
	 */
	@SysLog("地区-添加")
	@Authority(name = AuthorityCommon.ADD)
	@RequestMapping(value = "/save")
	public ModelAndView save(Area entity) throws Exception {
		ModelAndView mv = this.getModelAndView();
		entity.setId(IDUtils.getAreaId());
		areaService.save(entity);
		mv.addObject("msg", "success");
		mv.setViewName("save_result");
		return mv;
	}
	
	/**
	 * 删除
	 *
	 * @param out
	 * @throws Exception
	 */
	@SysLog("地区-删除")
	@Authority(name = AuthorityCommon.DELETE)
	@RequestMapping(value = "/delete")
	@ResponseBody
	public String delete(Long id) throws Exception {
		
		areaService.delete(id);
		return "success";
	}
	
	/**
	 * 修改
	 *
	 * @param
	 * @throws Exception
	 */
	@SysLog("提现-修改")
	@Authority(name = AuthorityCommon.EDIT)
	@RequestMapping(value = "/edit")
	public ModelAndView edit(Area entity) throws Exception {
		ModelAndView mv = this.getModelAndView();
		areaService.edit(entity);
		mv.addObject("msg", "success");
		mv.setViewName("save_result");
		return mv;
	}
	
	/**
	 * 列表
	 *
	 * @param page
	 * @throws Exception
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Authority(name = AuthorityCommon.LIST)
	@RequestMapping(value = "/list")
	@ResponseBody
	public RepBaseVO list(Page<Area> page, @RequestParam Map<String, Object> map) throws Exception {
		RepBaseVO repVO = new RepBaseVO<>();
		if (StringUtils.isBlank((String) map.get("type"))) {
			map.put("type", Area.TYPE_PROVINCE);
		}
		page = areaService.list(new Page<Area>(page.getCurrent(), page.getSize()), map);
		List<Area> varList = page.getRecords();
		Map<String, Object> resultInfo = new HashMap<>(2);
		resultInfo.put("data", varList);
		resultInfo.put("total", page.getTotal());
		return repVO.setCommonSuccess().setData(resultInfo);
	}
	
	/**
	 * 去新增页面
	 *
	 * @param
	 * @throws Exception
	 */
	@Authority(name = AuthorityCommon.ADD)
	@RequestMapping(value = "/goAdd")
	public ModelAndView goAdd(Long pid) throws Exception {
		ModelAndView mv = this.getModelAndView();
		Area pArea = areaService.findById(pid);
		
		mv.addObject("pArea", pArea);
		mv.setViewName("system/area/area_add");
		return mv;
	}
	
	/**
	 * 去修改页面
	 *
	 * @param
	 * @throws Exception
	 */
	@Authority(name = AuthorityCommon.EDIT)
	@RequestMapping(value = "/goEdit")
	public ModelAndView goEdit(Long id, @RequestParam Map<String, Object> map) throws Exception {
		ModelAndView mv = this.getModelAndView();
		
		Area entity = areaService.findById(id);
		mv.setViewName("system/area/area_edit");
		mv.addObject("msg", "edit");
		mv.addObject("entity", entity);
		mv.addObject("pd", map);
		return mv;
	}
	
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(format, true));
	}
}
